package com.st.mongodb;

import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;

public class SentiStatusCodec implements Codec<SentiStatus> {

  @Override
  public void encode(BsonWriter writer, SentiStatus value,
                     EncoderContext encoderContext) {

    writer.writeStartDocument();
    writer.writeString("mid", value.getMid());
    writer.writeString("text", value.getText());
    writer.writeString("retText", value.getRetText());
    writer.writeEndDocument();
  }

  @Override
  public Class<SentiStatus> getEncoderClass() {
    return SentiStatus.class;
  }

  @Override
  public SentiStatus decode(BsonReader reader, DecoderContext decoderContext) {
    SentiStatus sentiStatus = new SentiStatus();

    //和放入顺序有关 必须一致 且不能有多余的字段
    reader.readStartDocument();
    reader.readObjectId("_id");
    sentiStatus.setMid(reader.readString("mid"));
    sentiStatus.setText(reader.readString("text"));
    sentiStatus.setRetText(reader.readString("retText"));
    reader.readEndDocument();

    return sentiStatus;
  }
}
